﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic.OnlineSupports;
using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.OnlineSupports
{
    public partial class listOnlineSupports : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        string NịckName;
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            if (txtKeysearch.Text != "")
                NịckName = txtKeysearch.Text.Trim();
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize30 + 1;
            if (from == 1) from = 0;
            int to = (cPage) * Const.pageSize30;
            totalRecords = OnlineSupportsBL.GetCount(NịckName, Const.show);
            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize30;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "NickName", "from", "to", "cPage" },
                                    new string[] { NịckName, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "NickName", "from", "to" },
                                    new string[] { NịckName, from.ToString(), to.ToString() });
            CHRTList<OnlineSupportsInfo> list = OnlineSupportsBL.SearchAll(NịckName, Const.show, from, to);
            if (list != null && list.Count > 0)
            {
                grdOnlineSupports.Visible = true;
                grdOnlineSupports.DataSource = list;
                grdOnlineSupports.RowDataBound += new GridViewRowEventHandler(grdOnlineSupports_RowDataBound);
                grdOnlineSupports.DataBind();
                PagingAjax1.Visible = true;
            }
            else
            {
                grdOnlineSupports.Visible = false;
                lblMessage.Text = "Không tồn tại dữ liệu !";
                PagingAjax1.Visible = false;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void grdOnlineSupports_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int onlineSupportId = Convert.ToInt32(e.CommandArgument);
                            OnlineSupportsBL.Delete(onlineSupportId);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
           
        }

        protected void grdOnlineSupports_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            e.Row.Cells[0].Text = (Const.pageSize30 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
        }
    }
}